package com.abing.system.controller;

import com.abing.common.annotation.Log;
import com.abing.common.config.BootdoConfig;
import com.abing.common.controller.BaseController;
import com.abing.common.domain.FileDO;
import com.abing.common.domain.Tree;
import com.abing.common.service.FileService;
import com.abing.common.utils.MD5Utils;
import com.abing.common.utils.R;
import com.abing.common.utils.ShiroUtils;
import com.abing.system.domain.MenuDO;
import com.abing.system.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LoginController extends BaseController {


    @Autowired
    MenuService menuService;
    @Autowired
    FileService fileService;
    @Autowired
    BootdoConfig bootdoConfig;


    @GetMapping("/")
    String welcome(Model model){

        return "redirect:/login";
    }
    @GetMapping("/login")
    String login(Model model){
        model.addAttribute("username", bootdoConfig.getUsername());
        model.addAttribute("password", bootdoConfig.getPassword());
        return "login";
    }

    @Log("登录")
    @PostMapping("/login")
    @ResponseBody
    R ajaxLogin(String username,String password){

        password= MD5Utils.encrypt(username,password);
        UsernamePasswordToken token=new UsernamePasswordToken(username,password);
        Subject subject= SecurityUtils.getSubject();
        try {
            subject.login(token);
            return R.ok();
        } catch (AuthenticationException e) {
            return R.error("用户或密码错误");
        }
    }

    @Log("请求访问主页")
    @GetMapping("/index")
    String index(Model model){
        List<Tree<MenuDO>> menus=menuService.listMenuTree(getUserId());
        model.addAttribute("menus",menus);
        model.addAttribute("name",getUser().getName());
        FileDO fileDO=fileService.get(getUser().getPicId());
        if (fileDO!=null&&fileDO.getUrl()!=null){
            if (fileService.isExist(fileDO.getUrl())){
                model.addAttribute("picUrl",fileDO.getUrl());
            }else {
                model.addAttribute("picUrl","/img/photo_s.jpg");
            }
        }else {
            model.addAttribute("picUrl","/img/photo_s.jpg");
        }
        model.addAttribute("username",getUser().getUsername());
        return "index_v1";
    }
    @GetMapping("/main")
    String main() {
        return "main";
    }

    @GetMapping("/logout")
    String logout() {
        ShiroUtils.logout();
        return "redirect:/login";
    }
}
